package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.common.Result;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Order;
import com.example.entity.store;
import com.example.service.GoodsService;
import com.example.service.OrderService;
import com.example.service.storeService;
import com.example.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Order")
public class OrderController {

    @Autowired
    private OrderService OrderService;

    @Autowired
    private storeService storeService;

    @Autowired
    private GoodsService goodsService;


    @GetMapping("/list")
    public Result list(@RequestParam("status") String status) {
        // 根据当前身份判断 1.商家 就查询所有订单   2.用户 查询用户的订单
        Account currentUser = TokenUtils.getCurrentUser();

        List<Order> result = new ArrayList<>();

        if (currentUser.getRole().equals(RoleEnum.ADMIN.name())) {
            //1.商家 就查询所有订单
            //1.1 查询商家的所有店铺
            List<store> storeList = storeService.list(new QueryWrapper<store>()
                    .eq("business", currentUser.getId()));
            //1.2 根据店铺id 查询
            for (store store : storeList) {
                for (Order store_id : OrderService.list(new QueryWrapper<Order>()
                        .eq("store_id", store.getId())
                        .eq(status != null && status != "" && !status.equals("null"), "`status`", status))) {
                    result.add(store_id);
                }
            }
        } else {
            //2.用户 查询用户的订单
            result = OrderService.list(new QueryWrapper<Order>()
                    .eq("user_id", currentUser.getId())
                    .eq(!status.equals("null") && status != null, "`status`", status));
        }
        //  把店铺id 和 商品id 变成真实的数据
        for (Order order : result) {
            order.setGoods(goodsService.getById(order.getGoodId()));
            order.setStore(storeService.getById(order.getStoreId()));
        }
        return Result.success(result);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Order Order) {
        Account currentUser = TokenUtils.getCurrentUser();
        // 查询有没有订单
        List<Order> orderList = OrderService.list(new QueryWrapper<Order>().eq("user_id", currentUser.getId()));
        if (orderList.size() > 0) {
            //有订单
            for (Order order : orderList) {
                //判断是不是同一家门店
                if (Order.getStoreId() == order.getStoreId()) {
                    //同一个门店只能有一个未支付
                    if ("未支付".equals(order.getStatus())) {
                        return Result.error();
                    }
                }
            }
        }
        //没订单  去新增
        Order.setUserId(currentUser.getId());
        Order.setStatus("未支付");
        OrderService.save(Order);
        return Result.success();
    }


    @DeleteMapping("/remove/{id}")
    public Result removeByid(@PathVariable int id) {
        OrderService.removeById(id);
        return Result.success();
    }


    @PutMapping("/changeSta")
    public Result changeSta(@RequestBody Order order) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("`status`", order.getStatus());
        updateWrapper.eq("id", order.getId());
        OrderService.update(updateWrapper);
        return Result.success();
    }
}
